package funcInterface.consumer;

import java.util.function.BiFunction;

/**
 * BiFunction和Function类似，只不过它接收的是两个参数，返回一个结果
 */
public class BiFunctionTest {
    public static void main(String[] args) {
        BiFunctionTest biFunctionTest = new BiFunctionTest();

        System.out.println(biFunctionTest.add(2, 3));

        // 灵活性极大提升
        System.out.println(biFunctionTest.cal(2, 3, (x, y) -> x + y));
        System.out.println(biFunctionTest.cal(2, 3, (x, y) -> x * y));
        System.out.println(biFunctionTest.cal(2, 3, (x, y) -> x / y));
        System.out.println(biFunctionTest.cal(2, 3, (x, y) -> x - y));
    }

    public int add(int x, int y) {
        return x + y; // 具体行为是加操作
    }

    public int cal(int x, int y, BiFunction<Integer, Integer, Integer> biFunction) {
        return biFunction.apply(x, y); // 把具体行为抽象出来，交给调用者实现
    }
}
